package com.qqzone.qq.service.impl;

import com.qqzone.qq.dao.TopicDao;
import com.qqzone.qq.pojo.Reply;
import com.qqzone.qq.pojo.Topic;
import com.qqzone.qq.pojo.UserBasic;
import com.qqzone.qq.service.ReplyService;
import com.qqzone.qq.service.TopicService;
import com.qqzone.qq.service.UserBasicService;

import java.util.List;

public class TopicServiceImpl implements TopicService {

    private TopicDao topicDao;
    private ReplyService replyService;
    private UserBasicService userBasicService;

    @Override
    public List<Topic> getTopicList(UserBasic userBasic) {
        return topicDao.getTopicList(userBasic);
    }

    @Override
    public Topic getTopicById(Integer id) {
        Topic topic = getTopic(id);
        List<Reply> replyList = replyService.getReplyListByTopicId(topic.getId());
        topic.setReplyList(replyList);
        return topic;
    }

    @Override
    public Topic getTopic(Integer id) {
        Topic topic = topicDao.getTopic(id);
        UserBasic author = userBasicService.getUserBasicById(topic.getAuthor().getId());
        topic.setAuthor(author);
        return topic;
    }

    @Override
    public void delTopicById(Integer id) {
        Topic topic = getTopicById(id);
        if (topic != null) {
            if (topic.getReplyList() != null) {
                for (int i = 0; i < topic.getReplyList().size(); i++) {
                    Reply reply = topic.getReplyList().get(i);
                    replyService.delReply(reply.getId());
                }
            }
            topicDao.delTopic(new Topic(id));
        }
    }


}
